<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/webpage/include/taglib.jsp"%>
<html>
<head>
<script>
    var ctx = '${ctx}';
</script>
	<title>转库凭证管理</title>
	<meta name="decorator" content="ani"/>
	<%@ include file="/webpage/include/bootstraptable.jsp"%>
	<%@include file="/webpage/include/treeview.jsp" %>
	<script src="${ctxStatic}/common/js/common.js"></script> 
	<!-- 引入 echarts.js -->
	<%@ include file="/webpage/include/echarts.jsp"%>
	<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-gl/echarts-gl.min.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-stat/ecStat.min.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/extension/dataTool.min.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/china.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/map/js/world.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/extension/bmap.min.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/simplex.js"></script>
	<script type="text/javascript">

		$(document).ready(function() {
			$("#inputForm").validate({
				submitHandler: function(form){
					//jp.loading();
					//form.submit();
					$.ajax({
		                url:"${ctx}/transferevidence/transferEvidence/save",
		                type:"post",
		                data:$('#inputForm').serialize(),
		                beforeSend: function () {
		                    // 禁用按钮防止重复提交
		                    $("#submit").attr({ disabled: "disabled" });
		                },
		                success:function(data){
							if(data.code=='0'){
								jp.info(data.message);
								window.location.href="${ctx}/transferevidence/transferEvidence/list";
							}
							else{
								jp.warning(data.message);
							}
		                },
		                complete: function () {
		                    $("#submit").removeAttr("disabled");
		                },
		            });
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
		});
	</script>
</head>
<body>
<div class="wrapper wrapper-content">				
<div class="row">
	<div class="col-md-12">
	<div class="panel panel-primary">
		<div class="panel-heading">
			<h3 class="panel-title"> 
				<a class="panelButton" href="${ctx}/transferevidence/transferEvidence"><i class="ti-angle-left"></i> 返回</a>
			</h3>
		</div>
		
		<div class="panel-body">
		<form:form id="inputForm" modelAttribute="transferEvidence" action="${ctx}/transferevidence/transferEvidence/save" method="post" class="form-horizontal">
		<div class="form-group">
			<!-- <input id="conn"  type="button" class="btn btn-primary" value="闸机扫描" />
			<input id="phoneConn"  type="button" class="btn btn-primary" value="手机扫描" />
		    <input id="close" type="button" class="btn btn-success" value="识别" />
		    <input id="clear" type="button" class="btn btn-danger" value="清空" /> -->
		    <button id="submit" class="btn btn-info" data-loading-text="正在提交...">提 交</button>
		    <label id="isConnect" class="col-sm-2" style="display: none;">false</label>
		</div>    
		<form:hidden path="id"/>
		    
		<sys:message content="${message}"/>	
				<div class="form-group">
					<label class="col-sm-2 control-label">仓库类型：</label>
					<div class="col-sm-4">
						<form:select path="warehouseType" class="form-control " onchange="findWarehouse('warehouseId','warehouseType')" cssClass="form-control required">
							
						</form:select>
					</div>
					<label class="col-sm-2 control-label">仓库名称：</label>
					<div class="col-sm-4">
						<form:select path="warehouseId" class="form-control " cssClass="form-control required">
							
						</form:select>
					</div>
				</div>
				
				<div class="form-group">
					<label class="col-sm-2 control-label">转库原因：</label>
					<div class="col-sm-4">
						<form:select path="transferReason" class="form-control " cssClass="form-control required">
							<form:option value="" label=""/>
							<form:options items="${fns:getDictList('sc_transfer_reason')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
						</form:select>
					</div>
					<label class="col-sm-2 control-label">备注：</label>
					<div class="col-sm-4">
						<form:input path="comment" htmlEscape="false"  class="form-control "/>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 control-label">编码：</label>
					<div class="col-sm-10">
						<form:input path="resourceIds" onclick="openResourceView()" htmlEscape="false" cssClass="form-control required"   class="form-control "/>
					</div>
				</div>
				
				
					
	<div id="main" style="width: 100%;height:300px;display: none"></div>
	<div id="resourceDiv">
	<table id="checkResourceTable"  data-toolbar="#toolbar"></table>
		
	</div>
		 <c:if test="${fns:hasPermission('transferevidence:transferEvidence:edit') || isAdd}">
				<div class="col-lg-3"></div>
		        <div class="col-lg-6">
		             <div class="form-group text-center">
		                 <div>
		                 </div>
		             </div>
		        </div>
		</c:if> 
		
		</form:form>
		</div>				
	</div>
	</div>
	
</div>
<table id="table" style="display: none;" class="table table-hover table-striped">
	<thead>
		<tr>
			<th data-field="count">数量</th>
			<th data-field="sku">sku</th>			
			<th data-field="firstType">一级类目</th>
			<th data-field="secondType">二级类目</th>
			<th data-field="thirdType">三级类目</th>
		</tr>
	</thead>
	
</table>
<div id="resCount"></div>

</div>
<script type="text/javascript">
	findWarehouseType("warehouseType",["4"]);
</script>
<script type="text/javascript">
function openResourceView(){
	jp.open({
	    type: 2, 
	    area: ['80%', '80%'],
	    title:"成品库资源",
	    content:"${ctx}/sc/realtimeresource/statRealtimeResource",
	    btn: ['确定','关闭'],
	    btn1: function(index, layero){
	      var codeArr=new Array();
	      var arr=new Array();
	      var rows =$(layero).find("iframe")[0].contentWindow.$("#statRealtimeResourceTable").bootstrapTable('getSelections');
	      if(rows.length>=1){
	    	  $('#table').bootstrapTable('destroy');
	    	  $("#table").bootstrapTable({
	    		  
  	            formatLoadingMessage: function () {
  	                return "请稍等，正在加载中...";
  	            },
  	            formatNoMatches: function () {
  	                return '无符合条件的记录';
  	            }
  	        	});
	    	  $("#table").show(); 
	    	  for(var i=0;i<rows.length;i++){
	    		    codeArr.push(rows[i].id);
	    			arr.push(rows[i].productName+"##"+rows[i].firstType+"##"+rows[i].secondType+"##"+rows[i].thirdType);
	    		}
	    	  var obj=getWordCnt(arr);
	    	  
	    	  
	    	  jp.close(index);
	    	  for (var item in obj){ 		  
	    		  var index = $("#table").bootstrapTable('getData').length;
	    		  var arr=item.split("##");
	    		  $("#table").bootstrapTable('insertRow', {
	    	            index: index,
	    	            row: {                
	    	                count:obj[item],
	    	                sku:arr[0],
	    	                firstType:arr[1],
	    	                secondType:arr[2],
	    	                thirdType:arr[3]
	    	            }
	    	        });
	    	  }
	    	  $("#resourceIds").empty();
			  $("#resourceIds").val(codeArr.join(','));
			  
	      }
	     
	      
	      
	  }
	}); 
}

function getWordCnt(arr){ 
    var obj = {}; 
    for(var i= 0, l = arr.length; i< l; i++){ 
        var item = arr[i]; 
        obj[item] = (obj[item] +1 ) || 1; 
    } 
    return obj ; 
}

</script>

</body>
</html>